package com.qf.ssm.service.impl;

import com.github.pagehelper.PageInfo;
import com.qf.ssm.Cache.SceneCache;
import com.qf.ssm.event.CacheEvent;
import com.qf.ssm.exception.StudentIsEmptyException;
import com.qf.ssm.exception.StudentMsgException;
import com.qf.ssm.mapper.StudentMapper;
import com.qf.ssm.pojo.TbStudent;
import com.qf.ssm.service.StudentService;
import com.qf.ssm.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 王博康
 * @version 1.0
 * @project 07_ssm
 * @description: TODO
 * @date 2022/3/10 11:30 星期四
 */
@Service
@Transactional
public class StudentServiceImpl implements StudentService {
    private StudentMapper studentMapper;

    private ApplicationContext context;

    private SceneCache sceneCache;

    @Autowired
    public void setSceneCache(SceneCache sceneCache) {
        this.sceneCache = sceneCache;
    }

    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Autowired
    public void setStudentMapper(StudentMapper studentMapper) {
        this.studentMapper = studentMapper;
    }

    @Override
    public void addStu(TbStudent student) {
        Assert.isTrue(!student.isEmpty(),()->{
            throw new StudentIsEmptyException("学生信息不能为空",ResultCode.FAIL);
        } );
        studentMapper.addStu(student);
        context.publishEvent(new CacheEvent());
    }

    @Override
    public void updateStu(TbStudent student) {
        Assert.isTrue(student.getId()>0, ()->{
            throw new StudentMsgException("学生id应大于0",ResultCode.FAIL);
        });
        studentMapper.updateStu(student);
    }

    @Override
    public TbStudent findById(Long id) {
        Assert.isTrue(id>0, ()->{
            throw new StudentMsgException("学生id应大于0",ResultCode.FAIL);
        });
        return studentMapper.findById(id);
    }

    @Override
    public List<TbStudent> findByName(String name) {
        Assert.isTrue(!name.isEmpty(), ()->{
            throw new StudentMsgException("学生姓名不可为空", ResultCode.FAIL);
        });
        return studentMapper.findByName(name);
    }

    @Override
    public PageInfo<TbStudent> findAll(int pageSize, int pageNum) {
        /*
        仅调用mapper前开启分页有效，其原来通过更改sql语句实现，如果从缓存中拿需要自行裁剪
        PageHelper.startPage(pageNum, pageSize);
        List<TbStudent> all = studentMapper.findAll();
        */
        //得到所有数据
        List<TbStudent> studentList = sceneCache.getData();
        //裁剪数据
        List<TbStudent> pagestudents = studentList.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        //将裁剪后的数据放入pageinfo
        PageInfo<TbStudent> pageInfo = new PageInfo<>(pagestudents);
        //单独设置总长度
        pageInfo.setTotal(studentList.size());
        return pageInfo;
    }

}
